﻿/*Copyright (C) 2010-2012 RAAFCOM B.V. All rights reserved.

This file is part of Velsto and may be used under the terms of:
The GNU General Public License version 3 or higher, as published by
the Free Software Foundation.

This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.*/
/* 
* Application  : Velsto
* Class        : Kb2dParFile
* 18/10/2010   : M.A.van Ravesteijn (Raafcom)
*                Initial version
* 04/05/2012   : M.A.van Ravesteijn (Raafcom)
*                v1.1 added residual map
* 
* Class with method to interface SimulationEntity objects with gslib kriging
* routine(kb2d)
* .PAR file
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Globalization;
using System.Windows.Forms;

namespace Velsto
{
    public class Kb2dParFile
    {
        public Kb2dParFile()
        {
        }

        // Method to create input file for gslib kb2d_velsto run.
        public SimulationEntity saveKb2dParFile(SimulationEntity se)
        {
            try
            {
                TextWriter parFile = new StreamWriter("kb2d.par");
                parFile.WriteLine("START OF PARAMETERS");
                parFile.WriteLine(se.Kb2dControlPointsFile);
                parFile.WriteLine("1   2   3");
                parFile.WriteLine("-1.0e21   1.0e21");
                parFile.WriteLine("0");
                parFile.WriteLine("kb2d.dbg");
                parFile.WriteLine(se.Kb2dOutputFile);
                parFile.WriteLine(
                    Convert.ToString(se.MapNcols) + " " +
                    (se.MapXllcenter).ToString("0.0", CultureInfo.InvariantCulture) + " " +
                    (se.MapCellsize).ToString("0.0", CultureInfo.InvariantCulture)
                );
                parFile.WriteLine(
                    Convert.ToString(se.MapNrows) + " " +
                    (se.MapYllcenter).ToString("0.0", CultureInfo.InvariantCulture) + " " +
                    (se.MapCellsize).ToString("0.0", CultureInfo.InvariantCulture)
                );
                parFile.WriteLine("1     1");
                if (se.MapType == Constants.MAP_ZDEV_WELL ||
                    se.MapType == Constants.MAP_ZDEV_WELL2 || 
                    se.MapType == Constants.MAP_WELL_BULL)
                {
                    parFile.WriteLine("1     1");
                }
                else if (se.MapType == Constants.MAP_Z_WELL ||
                         se.MapType == Constants.MAP_RES_KRIG)
                {
                    //parFile.WriteLine("1     4");
                    parFile.WriteLine("1     " + se.NrWellLayers.ToString());
                }
                double searchrange = 0.0;
                if (se.MapType == Constants.MAP_ZDEV_WELL ||
                    se.MapType == Constants.MAP_ZDEV_WELL2 || 
                    se.MapType == Constants.MAP_WELL_BULL)
                {
                    searchrange = 2.0 * Math.Max(se.MapNcols, se.MapNrows) * se.MapCellsize;
                }
                else if (se.MapType == Constants.MAP_Z_WELL ||
                         se.MapType == Constants.MAP_RES_KRIG)
                {
                    //searchrange = se.WellResVarioRange;
                    searchrange = 2.0 * Math.Max(se.MapNcols, se.MapNrows) * se.MapCellsize;
                }
                parFile.WriteLine(searchrange.ToString("0.0", CultureInfo.InvariantCulture));
                if (se.WellResKrigingType == Constants.KRIGINGTYPE_ORDINARY)
                {
                    parFile.WriteLine("1    0.0");
                }
                else if (se.WellResKrigingType == Constants.KRIGINGTYPE_SIMPLE)
                {
                    if (se.MapType == Constants.MAP_Z_WELL ||
                        se.MapType == Constants.MAP_RES_KRIG)
                    {
                        parFile.WriteLine("0    " + se.Res_mean.ToString("0.0", CultureInfo.InvariantCulture));

                    } 
                    else if (se.MapType == Constants.MAP_ZDEV_WELL ||
                             se.MapType == Constants.MAP_ZDEV_WELL2 ||
                             se.MapType == Constants.MAP_WELL_BULL)
                    {
                        parFile.WriteLine("0    0.0");
                    }
                }
                double c0 = 0.0,c1=0.0, range=0.0;
                int model=1;
                if (se.MapType == Constants.MAP_ZDEV_WELL)
                {
                    c0 = 0.0;
                    c1 = 0.5;
                    range = se.Kb2dRange;
                    model = Constants.VAR_MODEL_SPHER;

                }
                else if (se.MapType == Constants.MAP_ZDEV_WELL2 ||
                         se.MapType == Constants.MAP_WELL_BULL) 
                {
                    c0 = se.WellResVarioC0;
                    c1 = 0.5;
                    range = se.WellResVarioRange;
                    model = se.WellResVarioModel;
                }
                else if (se.MapType == Constants.MAP_Z_WELL ||
                         se.MapType == Constants.MAP_RES_KRIG)
                {
                    c0 = se.WellResVarioC0;
                    c1 = se.WellResVarioC1;
                    range = se.WellResVarioRange;
                    model = se.WellResVarioModel;
                }
                parFile.WriteLine("1    " + c0.ToString("0.000", CultureInfo.InvariantCulture));
                parFile.WriteLine(model.ToString("0", CultureInfo.InvariantCulture) + "  " +
                    c1.ToString("0.000", CultureInfo.InvariantCulture) + " " +
                    "0.0" + " " +
                    range.ToString("0.0", CultureInfo.InvariantCulture) + " " +
                    range.ToString("0.0", CultureInfo.InvariantCulture)
                );                
                parFile.Close();
                se.ErrorMessage = null;
                return se;
            }
            catch (Exception ex)
            {
                se.ErrorMessage = ex.ToString();
                return se;
            }
        }
    }
}
